package com.hm.health.records.ecgpatch;

import com.goodix.iot.libbase.logger.Glog;
import com.hm.health.Utils.Constans;
import com.hm.health.entity.dao.EcgPatchSectorInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * EcgPatchSector代表flash中的一个sector的数据。Flash sector大小为4k,与一个package id对应，
 * 数据存储经过压缩，根据压缩率的不同，每一个sector包含的数量接近的frame数量。
 *
 * 该类会把收到一个sector，并根据frame id校验其完整性后，存成一个单独的文件，并将该sector的相关信息记录在数据中
 *
 * 文件分为两类，一类为record,用于历史记录显示,一类为server，用于原始数据保存。后续会考虑将这两类合为一类，节约空间。
 *一个sector数据包括多个frame数据，frame数据表示一次采样的数据，表示为一行
 *
 * 负责管理统计sector数据是否完成，提供结果给外部查询缺失的sector id
 */
public class EcgPatchSector {

    private final static String TAG = "EcgPatchSector";

    private List<EcgPatchFrame> mFramesOneSector = null;
    private int mTotalFrameNumOneSector = 0;
    private EcgPatchSectorInfo mSectorInfo = new EcgPatchSectorInfo();
    private String mSectorHeader = null;
    private String mSectorColumnDescription = null;
    private String mSectorResultColumnDescription = null;
    private String mSerializeResult = null;
    private String mSerializeRawdata = null;
    private boolean mIsSerialize = false;

    public EcgPatchSector(int sectorId, long beginTimeStamp, String testId, int mode, boolean isValid) {
        mSectorInfo.setSectorId(sectorId);
        mSectorInfo.setBeginTimeStamp(beginTimeStamp);
        mSectorInfo.setTestId(testId);
        mSectorInfo.setMode(mode);
        mSectorInfo.setValid(isValid);

        StringBuilder rawdataPath = new StringBuilder();
        rawdataPath.append(Constans.SECTOR_ROOT_FOLDER).append(File.separator).append(testId)
                .append(File.separator).append("server").append(File.separator).append(/*"." + */beginTimeStamp + "-" + sectorId + "-" + mode + ".csv");
        mSectorInfo.setSectorFilePath(rawdataPath.toString());

        StringBuilder resultPath = new StringBuilder();
        resultPath.append(Constans.SECTOR_ROOT_FOLDER).append(File.separator).append(testId)
                .append(File.separator).append("record").append(File.separator).append(/*"." + */beginTimeStamp + "-" + sectorId + "-" + mode + ".csv");
        mSectorInfo.setSectorResultFilePath(resultPath.toString());

        mFramesOneSector = new ArrayList<EcgPatchFrame>();
    }

    public void setSectorHeader(String header) {
        mSectorHeader = header;
    }

    public String getSectorHeader() {
        return mSectorHeader;
    }

    public void setSectorColumnDescription(String description) {
        mSectorColumnDescription = description;
    }

    public String getSectorColumnDescription() {
        return mSectorColumnDescription;
    }

    public void setSectorResultColumnDescription(String description) {
        mSectorResultColumnDescription = description;
    }

    public String getSectorResultColumnDescription() {
        return mSectorResultColumnDescription;
    }

    public List<EcgPatchFrame> getFrames() {
        return mFramesOneSector;
    }

    public boolean addFrame(EcgPatchFrame frame) {

        if (frame == null) {
            Glog.e(TAG, "oh no! frame can not be null");
            return false;
        }

        if (frame.mSectorId != mSectorInfo.getSectorId()) {
            Glog.e(TAG, "frame id:" + frame.mSectorId + " is not equal sector id:" + mSectorInfo.getSectorId());
            return false;
        }

        mFramesOneSector.add(frame);

        // use frame timestamp to be end time
        mSectorInfo.setEndTimeStamp(frame.mTimestamp);

        return true;
    }

    public EcgPatchSectorInfo getSectorInfo() {
        return mSectorInfo;
    }

    public boolean checkSectorFramesAllReceived() {
        if (mTotalFrameNumOneSector == 0 || mFramesOneSector.size() == 0)
            return false;
        return mTotalFrameNumOneSector == mFramesOneSector.size();
    }

    public boolean checkSectorValid() {

        boolean result = true;

        if (mTotalFrameNumOneSector != mFramesOneSector.size()) {
            Glog.e(TAG, "sector is not valid yet, total num:" + mTotalFrameNumOneSector + " is not equal frame list size:" + mFramesOneSector.size());
            result = false;
        }

        return result;
    }

    public void setFrameTotalNum(int num) {
        mTotalFrameNumOneSector = num;
    }

    public int getFrameTotalNum() {
        return mTotalFrameNumOneSector;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("EcgPatchSector:{ info:").append(mSectorInfo.toString())
                .append(", frame num:").append(mFramesOneSector.size()).append("}");
        return builder.toString();
    }

    static class EcgPatchFrame {
        public long mTimestamp;
        public int mFrameId;
        public int mSectorId;
        public int[] mRawdata;
        public int[] mRecord;
        public int mMode;

        public void setRawdata(int[] data) {
            if (data != null) {
                mRawdata = new int[data.length];
                System.arraycopy(data, 0, mRawdata, 0, data.length);
            }
        }

        public void setRecord(int[] data) {
            if (data != null) {
                mRecord = new int[data.length];
                System.arraycopy(data, 0, mRecord, 0, data.length);
            }
        }
    }

}